from nodetypes.formats.json.Events import Document

from myelin.Ns import MYELINS
from nodetypes.formats.json.Profiler import AgentProfiler

from nodetypes.models.Document import Document as Model


class Document(Document):
    
    newDocumentModel = Model.Retrieve
    newDocumentDriver = staticmethod(Model.Initialize)
    
    newProfiler = AgentProfiler
    
    def createBody(self, context, obj):
        eventData = context.event.data 
        
        params = eventData.params
        results = eventData.results
        
        #super(Form,self).createInstance(
        #    context,
        #    nxa(
        #        nxc(
        #            u'a:search-params', MYELINS
        #        )
        #    )
        #)
        
        header = eventData.resultsHeader
        if header.size:
            obj['results'] = {
                'context': {
                    u'total'        : header.estimate,
                    u'count'        : params['count'],
                    u'start'        : eventData.searchBaseIndex + 1,
                    u'end'          : eventData.searchBaseIndex + header.size,
                    u'page'         : params['page'],                    
                },
                'nodes' : self.createResults(
                    context,
                    [],
                    results,
                    params,
                    eventData
                )
            }
        else:
            obj['results'] = None
        
        return obj    
    
    def createResults(self, context, nodes, results, params, eventData):
        
        root = context.defaults.document_root
        map = context.uriMapper.apply
        
        locale = eventData.searchLocale or \
            context.preferences['locale']
                
        # FIX ME
        prof = self.newProfiler(root, locale)
        cap = prof.createProfile
        
        F = context.repo.fetchResource
        newDocModel = self.newDocumentModel
        newDocDriver = self.newDocumentDriver
        uriScopeResolver = context.uriScopeResolver
                
        nop = nodes.append
        for r in results:
            
            n = F(r.path)
            d = n.getAbsolutePathAsUri()
            n = newDocModel(
                newDocDriver(
                    n.getModel().driver,
                    uriScopeResolver = uriScopeResolver
                ),
                n.uri,
                d,d
            )            
            
            n_values = n.rdf_values
            cur = {
                'nodeId'    : n.rdf_value,                                     
                'uri'       : map(n.rdf_domain),                
                'lang'      : n_values('dcLanguage'),
                'tag'       : n_values('mTag'),
                'created'   : n_values('dctermsCreated'),   
                'title'     : n_values('dcTitle'),
                'desc'      : n_values('dcDescription'),
            }
                        
            cur['creator'] = [
                cap(
                    context,
                    {},
                    a
                )
                for a in n.dcCreator
            ]
            
            a = n_values('dctermsModified')
            if a:
                cur['modified'] = a
                cur['lastModifier'] = [
                    cap(
                        context,
                        {},
                        a
                    )
                    for a in n.mLast_Modifier
                ]
                    
            nop(cur)
            
        return nodes